package com.bytedance.frameworks.core.a;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

public final class d {
    private static final Object b;
    private static d a;
    private static String[] c;

    static {
        b = new Object();
        c = new String[]{"_id", "monitor_log_type", "monitor_value"};
    }

    private SQLiteDatabase d;

    private d(Context arg3) {

        try {
            this.d = new a(arg3).getWritableDatabase();
        } catch (Throwable v0) {
            Log.i("monitorDBHelper", "monitor DB build failed");
        }
    }

    public static d a(Context arg2) {
        Object v1 = b;
        synchronized (v1) {
            if (a == null) {
                a = new d(arg2);
            }
        }
        return a;
    }

    private static void a(Cursor arg4) {
        if (arg4 != null) {
            try {
                if (arg4.isClosed()) {
                    return;
                }

                arg4.close();
            } catch (Exception v0) {
                Log.e("monitorDBHelper", "cursor close error:" + v0);
            }
        }
    }

    public final synchronized long a(String arg5, String arg6) {
        if (this.d == null) {
        } else {
            ContentValues v0_2 = new ContentValues();
            v0_2.put("monitor_log_type", arg5);
            v0_2.put("monitor_value", arg6);
            long v0_1 = this.d.insert("monitor_log", null, v0_2);
            return v0_1;
        }

        long v0_1 = -1;
        return v0_1;
    }

    final synchronized long a() {
        long v0 = -1;
        Cursor v2 = null;
        try {
            if (this.d == null) {
                return v0;
            }

            if (this.d.isOpen()) {
//                goto label_10;
            } else {
                return v0;
            }

            String v3_1 = "select count(*) from monitor_log";
            if (!TextUtils.isEmpty(null)) {
                v3_1 = v3_1 + " " + null;
            }

            v2 = this.d.rawQuery(v3_1, null);
            if (v2.moveToNext()) {
                v0 = v2.getLong(0);
            }

            a(v2);
        } catch (Exception v3) {
            try {
                a(v2);
                return v0;
            } catch (Throwable v0_1) {

            }
        }
        return v0;
    }

    public final synchronized long a(JSONObject arg14) {
        String v12;
        long v4;
        JSONArray v8;
        JSONArray v7;
        JSONArray v6;
        JSONArray v2_1;
        Throwable v2;
        Cursor v3 = null;
        long v10 = -1;
        Cursor v9 = null;
        long v0_1;
        if (this.d != null) {
//                goto label_8;
        } else {
            v0_1 = v10;
            return v0_1;
        }
        try {
            label_8:
            v3 = this.d.query("monitor_log", c, null, null, null, null, "_id ASC", "100");
            v2_1 = new JSONArray();
            v6 = new JSONArray();
            v7 = new JSONArray();
            v8 = new JSONArray();
            v0_1 = v10;
            while (true) {
                label_28:
                if (!v3.moveToNext()) {
                    if (v2_1.length() > 0) {
                        arg14.put("data", v2_1);
                    }

                    if (v6.length() > 0) {
                        arg14.put("count", v6);
                    }

                    if (v7.length() > 0) {
                        arg14.put("timer", v7);
                    }

                    if (v8.length() > 0) {
                        arg14.put("store", v8);
                    }

                    a(v3);
                    return v0_1;
                }

                v4 = v3.getLong(0);
                String v9_1 = v3.getString(1);
                v12 = v3.getString(2);
                if (v0_1 < v4) {
                    v10 = v4;
                } else {
                    v10 = v0_1;
                }
                JSONObject v0_3 = new JSONObject(v12);
                if (v9_1.equals("count")) {
                    v6.put(v0_3);
                    v0_1 = v10;
                    continue;
                }

                if (v9_1.equals("timer")) {
                    v7.put(v0_3);
                    v0_1 = v10;
                    continue;
                }

                if (v9_1.equals("store")) {
                    v8.put(v0_3);
                    v0_1 = v10;
                    continue;
                }

                v0_3.put("log_id", v4);
                v2_1.put(v0_3);
                v0_1 = v10;
                continue;
            }
        } catch (Throwable throwable) {
            Log.e("monitorDBHelper", "obtain monitor logs failed:" + throwable.toString());
        } finally {
            a(v3);
        }
        return -1L;
    }

    public final synchronized void a(long arg6) {
        if (this.d != null) {
            String[] v1 = new String[]{"0"};
            v1[0] = String.valueOf(arg6);
            this.d.delete("monitor_log", "_id<= ? ", v1);
        }
    }

    final class a extends SQLiteOpenHelper {
        public a(Context arg4) {
            super(arg4, "ss_app_monitor.db", null, 1);
        }

        public final void onCreate(SQLiteDatabase arg5) {
            try {
                arg5.execSQL("CREATE TABLE monitor_log ( _id INTEGER PRIMARY KEY AUTOINCREMENT, monitor_log_type VARCHAR, monitor_value TEXT )");
            } catch (Exception v0) {
                Log.e("monitorDBHelper", "monitorDB create failed:" + v0);
            }
        }

        public final void onUpgrade(SQLiteDatabase arg1, int arg2, int arg3) {
        }
    }
}

